package codeGen.core;

import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;


public class BeanUtil {
    public static final String enter = "\r\n";

    private static Properties props1 = new Properties();
    private static Properties props2 = new Properties();

    static {
        try {
            props1.load(BeanUtil.class.getClassLoader().getResourceAsStream("mapping_sql2java.properties"));
            props2.load(BeanUtil.class.getClassLoader().getResourceAsStream("mapping_java2dwz.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String toJavaType(String sqlType) {
        String pureSqlType = sqlType.replaceAll("\\(\\d+\\)", "");
        String javaType = props1.getProperty(pureSqlType);
        if (null != javaType) return javaType;
        return "String";
    }

    public static String toDwzValidateClass(String javaType) {
        String dwzClass = props2.getProperty(javaType);
        if (null != dwzClass) return dwzClass;
        return "";
    }

    public static String toMethodName(String fieldName) {
        StringBuffer sb = new StringBuffer();
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        return sb.toString();
    }

    public static String toMethodNameCamelCase(String fieldName) {
        String str = toClassName(fieldName);
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static String toClassName(String tableName) {
        String[] sp = tableName.split("[-|_]");
        StringBuffer sb = new StringBuffer();
        for (String s : sp) {
            if (s == null || "".equals(s.trim()) || "t".equals(s) || "T".equals(s))
                continue;
            sb.append(s.substring(0, 1).toUpperCase());
            sb.append(s.substring(1));
        }
        return sb.toString();
    }

    public static String toClassInstanceName(String tableName) {
        String className = toClassName(tableName);
        return className.substring(0, 1).toLowerCase() + className.substring(1);
    }

    public static String toDaoInterfaceName(String tableName) {
        return toClassName(tableName) + "Dao";
    }

    public static String toDaoClassName(String tableName) {
        return toClassName(tableName) + "DaoImpl";
    }

    public static String toDaoInstanceName(String tableName) {
        return toClassName(tableName).toLowerCase() + "Dao";
    }

    public static String toServiceInterfaceName(String tableName) {
        return toClassName(tableName) + "Service";
    }

    public static String toServiceClassName(String tableName) {
        return toClassName(tableName) + "ServiceImpl";
    }

    public static String toServiceInstanceName(String tableName) {
        return toClassName(tableName).toLowerCase() + "Service";
    }

    public static String toActionClassName(String tableName) {
        return toClassName(tableName) + "Action";
    }
}
